package com.jinglvhuanjing.app.utils;


import com.example.x6.serial.SerialPort;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

//饿汉模式：（线程安全）
public class SingletonSerl {

    private String TAG = "SingletonSerl";
    private static SerialPort serialPortLogin = null;
    private static SerialPort serialPort = null;
    // 静态私有成员变量
    private static SingletonSerl instance = new SingletonSerl();

    // 投口类型key + 投口地址
    public static final String tk_string_start = "tk_string_start_";
    // 投口距离值key + 投口地址
    public static final String tk_jlz_string_start = "tk_jlz_string_start";
    // 投口轮询内容key + 投口地址
    public static final String tk_lunxun_string_start = "tk_lunxun_string_start";
    /** 5个投口地址 */
    public static byte[] bytes_tk = {(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05};
    public static byte byte_tk1 = (byte) 0x01;// 转为字符串就是01
    public static byte byte_tk2 = (byte) 0x02;// 转为字符串就是02
    public static byte byte_tk3 = (byte) 0x03;// 转为字符串就是03
    public static byte byte_tk4 = (byte) 0x04;// 转为字符串就是04
    public static byte byte_tk5 = (byte) 0x05;// 转为字符串就是05

    // 转为字符串就是03 - 首次轮询功能码，用于查找对应投口是 其它垃圾、厨余垃圾、可回收垃圾。
    public static byte byte_gnm_type = (byte) 0x03;
    // 参数设置
    public static byte byte_gnm_cssz = (byte) 0x06;
    // 开门指令
    public static byte byte_gnm_khsOrCy = (byte) 0x10;
    // 臭气值
    public static byte byte_cqz = (byte) 0x82;
    // 开门前指令
    public static byte[] byte_gnm_open_first = {(byte) 0x06, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x02};
    // 开门后指令
    public static byte[] byte_gnm_open_end = {(byte) 0x06, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x01};

    // 投口类型： 0x01其他垃圾，0x02，厨余垃圾，0x03可回收垃圾，0x04纸张，0x05金属，0x06塑料，0x07玻璃
    public static byte byte_tk_qt = (byte) 0x01;// 转为字符串就是01
    public static byte byte_tk_cy = (byte) 0x02;// 转为字符串就是02
    public static byte byte_tk_khs = (byte) 0x03;// 转为字符串就是03
    public static byte byte_tk_zz = (byte) 0x04;// 转为字符串就是04
    public static byte byte_tk_js = (byte) 0x05;
    public static byte byte_tk_sl = (byte) 0x06;
    public static byte byte_tk_bl = (byte) 0x07;

    // 私有构造函数
    private SingletonSerl() {
    }

    // 静态公有工厂方法，判断成员变量是否为空，不为空则实例化
    public static SingletonSerl getInstance() throws IOException {
        return instance;
    }

    /**
     * 获取前部分投口轮询数据下发指令报文
     * 投口地址  01: 1号投口；02: 2号投口；03: 3号投口；04: 4号投口；05: 5号投口；
     * @param dizhi
     */
    public StringBuffer getTK_(String dizhi){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_type));// 功能码
        buffer.append("00");// 起始地址
        buffer.append("0A");// 起始地址
        buffer.append("00");// 寄存器数
        buffer.append("08");// 寄存器数
        return buffer;
    }

    /**
     * 获取前部分投口轮询数据下发指令报文
     * @param dizhi  投口地址  01: 1号投口；02: 2号投口；03: 3号投口；04: 4号投口；05: 5号投口；
     * @param closeTime  投口关闭间隔
     */
    public StringBuffer openDoor(String dizhi, String closeTime){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_khsOrCy));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("01");// 寄存器地址
        buffer.append("00");
        buffer.append("02");
        buffer.append("04");

        buffer.append("00");
        buffer.append("03");// 1：正转(开门) 2：停止，3：反转

        buffer.append(closeTime);// 占用4个字节
        return buffer;
    }

    /**
     * 运行状态查询
     * @param dizhi
     * @return
     */
    public StringBuffer deviceRunType(String dizhi){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_type));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("0A");// 寄存器地址
        buffer.append("00");
        buffer.append("08");
        return buffer;
    }

    /**
     * 参数设置-投口类型设置
     * @param dizhi
     * @param tklx
     * @return
     */
    public StringBuffer deviceSettingTK(String dizhi, String tklx){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("0A");// 寄存器地址
        buffer.append("00");
        buffer.append(tklx);
        return buffer;
    }

    /**
     * 参数设置-距离限值设置
     * @param dizhi
     * @param jlxz
     * @return
     */
    public StringBuffer deviceSettingJLXZ(String dizhi, String jlxz){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("0C");// 寄存器地址
        buffer.append(jlxz);
        return buffer;
    }

    /**
     * 参数设置-电流限值设置
     * @param dizhi
     * @param dlxz
     * @return
     */
    public StringBuffer deviceSettingDLXZ(String dizhi, String dlxz){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("0E");// 寄存器地址
        buffer.append(dlxz);
        return buffer;
    }

    /**
     * 参数设置-闭门时间设置
     * @param dizhi
     * @param bmsj
     * @return
     */
    public StringBuffer deviceSettingBMSJ(String dizhi, String bmsj){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("10");// 寄存器地址
        buffer.append(bmsj);
        return buffer;
    }

    /**
     * 其它设置-清除障碍
     * @param dizhi
     * @return
     */
    public static StringBuffer otherSettingQCZA(String dizhi){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("06");// 寄存器地址
        buffer.append("00");// 清除故障
        buffer.append("01");// 清除故障
        return buffer;
    }

    /**
     * 其它设置-0点校准
     * @param dizhi
     * @return
     */
    public static StringBuffer otherSettingJZ(String dizhi){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("04");// 寄存器地址
        buffer.append("00");// 0点校准
        buffer.append("00");// 0点校准
        return buffer;
    }

    /**
     * 其它设置-去皮
     * @param dizhi
     * @return
     */
    public static StringBuffer otherSettingQP(String dizhi){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("04");// 寄存器地址
        buffer.append("FF");// 去皮
        buffer.append("FF");// 去皮
        return buffer;
    }

    /**
     * 其它设置-砝码校准
     * @param dizhi
     * @param fmjz
     * @return
     */
    public static StringBuffer otherSettingFMJZ(String dizhi, String fmjz){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("04");// 寄存器地址
        buffer.append(fmjz);
        return buffer;
    }

    /**
     * 其它设置-模式设置
     * @param dizhi
     * @param mssz
     * @return
     */
    public static StringBuffer otherSettingMSSZ(String dizhi, String mssz){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("05");// 寄存器地址
        buffer.append("00");// 模式设置
        buffer.append(mssz);// 模式设置
        return buffer;
    }

    /**
     * 其它设置-正转/反转
     * @param dizhi
     * @param ycsj   延迟时间
     * @param zj     正转/反转
     * @return
     */
    public static StringBuffer otherSettingZFZ(String dizhi, String ycsj, boolean zj){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_khsOrCy));// 功能码
        buffer.append("00");// 寄存器地址
        buffer.append("01");// 寄存器地址
        buffer.append("00");
        buffer.append("02");
        buffer.append("04");
        buffer.append("00");
        buffer.append(zj?"01":"03");
        buffer.append(ycsj);// 延迟时间
        return buffer;
    }

    /**
     * 获取开门前或开门后指令报文
     * @param dizhi
     * @param first
     * @return
     */
    public StringBuffer openDoorFirstOrEnd(String dizhi, boolean first){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        if(first){
            buffer.append(ByteUtil.hexBytesToString(byte_gnm_open_first));// 开门前
        }else{
            buffer.append(ByteUtil.hexBytesToString(byte_gnm_open_end));// 开门后
        }
        return buffer;
    }

    /**
     * 变更投口地址
     * @param nowAddress  当前地址
     * @param toAddress   目标地址
     */
    public StringBuffer changeAddress(String nowAddress, String toAddress){
        StringBuffer buffer = new StringBuffer();
        buffer.append(nowAddress);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器数
        buffer.append("E0");// 寄存器数
        buffer.append("00");
        buffer.append(toAddress);
        return buffer;
    }

    /**
     * 输出控制
     * @param dizhi     投口地址
     * @param kongzhi   控制值
     */
    public StringBuffer signalOutPanel(String dizhi, String kongzhi){
        StringBuffer buffer = new StringBuffer();
        buffer.append(dizhi);
        buffer.append(ByteUtil.hexByteToString(byte_gnm_cssz));// 功能码
        buffer.append("00");// 寄存器数
        buffer.append("03");// 寄存器数
        buffer.append("00");
        buffer.append(kongzhi);
        return buffer;
    }

    /**
     * 臭气值获取报文
     */
    public StringBuffer cqz(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("82");
        buffer.append("03");
        buffer.append("00");// 起始地址
        buffer.append("00");// 起始地址
        buffer.append("00");// 寄存器数
        buffer.append("07");// 寄存器数
        return buffer;
    }

    /**
     * 获取串口连接对象
     * 监听ttyS2，解析ttyS2的数据（ascii解析），得到的数据用于登录（暂时匹配为：18661140822）
     *
     * @return
     */
    public SerialPort getSerialPortConnect2() {
        try {
            if (serialPortLogin == null) {
                serialPortLogin = new SerialPort(new File("/dev/ttyS3"), 9600, 0);
                LOG.e(TAG, "登录串口 首次初始化:" + (serialPortLogin != null));
            }
        } catch (Exception e) {
            LOG.e(TAG, "登录串口 首次初始化 异常:" + (e.toString()));
            try {
                //默认su路径是/system/bin/su，有些设备su路径是/system/xbin/su
                //在new SerialPort();之前设置su路径
                serialPortLogin = new SerialPort(new File("/dev/ttyS3"), 9600, 0);
                LOG.e(TAG, "登录串口 首次初始化 再次初始化:" + (serialPortLogin != null));
            } catch (Exception e1) {
                serialPortLogin = null;
                return null;
            }
        } catch (UnsatisfiedLinkError e) {
            serialPortLogin = null;
            LOG.e(TAG, "登录串口 首次初始化 UnsatisfiedLinkError 异常:" + (e.toString()));
        }
        return serialPortLogin;
    }

    /**
     * 获取串口连接对象
     * 监听ttyS3，解析ttyS3的数据（ascii解析）
     *
     * @return
     */
    public SerialPort getSerialPortConnect3() {
        try {
            if (serialPort == null) {
                serialPort = new SerialPort(new File("/dev/ttyS4"), 9600, 0);
                LOG.e(TAG, "首次初始化:" + (serialPort != null));
            }
        } catch (Exception e) {
            LOG.e(TAG, "首次初始化 异常:" + (e.toString()));
            try {
                //默认su路径是/system/bin/su，有些设备su路径是/system/xbin/su
                //在new SerialPort();之前设置su路径
                serialPort = new SerialPort(new File("/dev/ttyS4"), 9600, 0);
                LOG.e(TAG, "首次初始化 再次初始化:" + (serialPort != null));
            } catch (Exception e1) {
                serialPort = null;
                return null;
            }
        } catch (UnsatisfiedLinkError e) {
            serialPort = null;
            LOG.e(TAG, "首次初始化 UnsatisfiedLinkError 异常:" + (e.toString()));
        }
        return serialPort;
    }

    /**
     * 写入数据（发送指令）
     *
     * @param hexString 16进制字符串
     */
    public void writeHexStringLogin(String hexString) {
        LOG.e(TAG, "写入数据(字符串):" + hexString);
        OutputStream outputStream = null;
        try {
            //从串口对象中获取输出流
            outputStream = serialPortLogin.getOutputStream();
            //写入数据
            outputStream.write(ByteUtil.hexStringToBytes(hexString));
            outputStream.flush();
        } catch (Exception e) {
            LOG.e(TAG, "写入数据(异常):" + e.toString());
            //写入数据
            try {
                outputStream.flush();
            } catch (Exception e1) {
            }
        }
    }

    /**
     * 写入数据（发送指令）
     *
     * @param hexString 16进制字符串
     */
    public void writeHexString(String hexString) {
        LOG.e(TAG, "写入数据(字符串):" + hexString);
        OutputStream outputStream = null;
        try {
            //从串口对象中获取输出流
            outputStream = serialPort.getOutputStream();
            //写入数据
            outputStream.write(ByteUtil.hexStringToBytes(hexString));
            outputStream.flush();
        } catch (Exception e) {
            LOG.e(TAG, "写入数据(异常):" + e.toString());
            //写入数据
            try {
                outputStream.flush();
            } catch (Exception e1) {
            }
        }
    }

    public String write(String TAG, StringBuffer buffer) {
        sleep();
        String end = getCRC3(ByteUtil.hexStringToBytes(buffer.toString()));
        LOG.e(TAG, "write crc16:" + end);
        buffer.append(end);
        LOG.e(TAG, "end=" + buffer.toString());
        if (hasSingletonSerl()) {
            writeHexString(buffer.toString());
            LOG.e(TAG, "write 结束");
        }
        return buffer.toString();
    }

    public void sleep(){
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 非登录串口对象判断
     *
     * @return
     */
    public boolean hasSingletonSerl() {
        SerialPort serialPort = getSerialPortConnect3();
        if (serialPort != null) {
            return true;
        }
        return false;
    }

    /**
     * 计算CRC16校验码
     *
     * @param bytes
     * @return
     */
    public static String getCRC(byte[] bytes) {
        int CRC = 0x0000ffff;
        int POLYNOMIAL = 0x0000a001;

        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        return Integer.toHexString(CRC);
    }

    /**
     * 查表法计算CRC16校验
     *
     * @param data 需要计算的字节数组
     */
    public static String getCRC3(byte[] data) {
        byte[] crc16_h = {
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,
                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40
        };

        byte[] crc16_l = {
                (byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, (byte) 0xC4, (byte) 0x04,
                (byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, (byte) 0x08, (byte) 0xC8,
                (byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, (byte) 0x1C, (byte) 0xDC,
                (byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, (byte) 0xD0, (byte) 0x10,
                (byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, (byte) 0x34, (byte) 0xF4,
                (byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, (byte) 0xF8, (byte) 0x38,
                (byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, (byte) 0xEC, (byte) 0x2C,
                (byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, (byte) 0x20, (byte) 0xE0,
                (byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, (byte) 0x64, (byte) 0xA4,
                (byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, (byte) 0xA8, (byte) 0x68,
                (byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, (byte) 0xBC, (byte) 0x7C,
                (byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, (byte) 0x70, (byte) 0xB0,
                (byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, (byte) 0x94, (byte) 0x54,
                (byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, (byte) 0x58, (byte) 0x98,
                (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, (byte) 0x4C, (byte) 0x8C,
                (byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, (byte) 0x80, (byte) 0x40
        };

        int crc = 0x0000ffff;
        int ucCRCHi = 0x00ff;
        int ucCRCLo = 0x00ff;
        int iIndex;
        for (int i = 0; i < data.length; ++i) {
            iIndex = (ucCRCLo ^ data[i]) & 0x00ff;
            ucCRCLo = ucCRCHi ^ crc16_h[iIndex];
            ucCRCHi = crc16_l[iIndex];
        }

        crc = ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff;
        //高低位互换，输出符合相关工具对Modbus CRC16的运算
        crc = ( (crc & 0xFF00) >> 8) | ( (crc & 0x00FF ) << 8);
        return String.format("%04X", crc);
    }



    /**
     * 十进制点击状态值解析为字符串
     * @param djzt
     * @return
     */
    public static String getDjzt(int djzt){
        String end = "异常";
        switch (djzt){
            case 0:
                end = "空闲";
                break;
            case 1:
                end = "正转";
                break;
            case 2:
                end = "刹车";
                break;
            case 3:
                end = "反转";
                break;
            case 4:
                end = "电机断路";
                break;
            case 5:
                end = "电机堵转";
                break;
            case 6:
                end = "电机顶点位";
                break;
            case 7:
                end = "电机原点位";
                break;
        }
        return end;
    }

    public static String getTklx(String type){
        String tklx = "未知";
        if(type.equals(ByteUtil.hexByteToString(SingletonSerl.byte_tk_qt))){
            tklx = "其它垃圾";
        }else if(type.equals(ByteUtil.hexByteToString(SingletonSerl.byte_tk_cy))){
            tklx = "厨余垃圾";
        }else if(type.equals(ByteUtil.hexByteToString(SingletonSerl.byte_tk_khs))){
            tklx = "可回收垃圾";
        }else if(type.equals(ByteUtil.hexByteToString(SingletonSerl.byte_tk_zz))){
            tklx = "纸张";
        }else if(type.equals(ByteUtil.hexByteToString(SingletonSerl.byte_tk_js))){
            tklx = "金属";
        }else if(type.equals(ByteUtil.hexByteToString(SingletonSerl.byte_tk_sl))){
            tklx = "塑料";
        }else if(type.equals(ByteUtil.hexByteToString(SingletonSerl.byte_tk_bl))){
            tklx = "玻璃";
        }
        return tklx;
    }

    /**
     * 寄存器地址翻译-参数设置
     * @param jcqdz
     * @return
     */
    public static String getJCQDZLX_CSSZ(String jcqdz){
        String lx = "未知";
        if(jcqdz.equals("000A")){
            lx = "投口类型";
        }else if(jcqdz.equals("000C")){
            lx = "距离限值";
        }else if(jcqdz.equals("000E")){
            lx = "电流限值";
        }else if(jcqdz.equals("0010")){
            lx = "闭门时间";
        }
        return lx;
    }

    /**
     * 寄存器地址翻译-输出控制
     * @param jcqdz
     * @return
     */
    public static String getJCQDZLX_SCKZ(String jcqdz){
        String lx = "未知";
        if(jcqdz.equals("0003")){
            lx = "输出控制";
        }
        return lx;
    }

    /**
     * 寄存器地址翻译-地址更改
     * @param jcqdz
     * @return
     */
    public static String getJCQDZLX_DZGG(String jcqdz){
        String lx = "未知";
        if(jcqdz.equals("00E0")){
            lx = "地址更改";
        }
        return lx;
    }

    /**
     * 寄存器地址翻译
     * @param jcqdz
     * @return
     */
    public static String getJCQDZLX(String jcqdz){
        String lx = "未知";
        if(jcqdz.equals("000A")){
            lx = "投口类型";
        }else if(jcqdz.equals("000C")){
            lx = "距离限值";
        }else if(jcqdz.equals("000E")){
            lx = "电流限值";
        }else if(jcqdz.equals("0010")){
            lx = "闭门时间";
        }else if(jcqdz.equals("0006")){
            lx = "清除故障";
        }else if(jcqdz.equals("0003")){
            lx = "输出控制";
        }else if(jcqdz.equals("00E0")){
            lx = "地址更改";
        }
        return lx;
    }

    /**
     * 根据中文类型，判断是否对应寄存器
     * @param lx
     * @return
     */
    public static String getJCQDZFromLX(String lx, String jcqdz){
        String end = "失败";
        if(jcqdz.equals("000A")&&lx.equals(getJCQDZLX(jcqdz))){
//            lx = "投口类型";
            end = "成功";
        }else if(jcqdz.equals("000C")&&lx.equals(getJCQDZLX(jcqdz))){
//            lx = "距离限值";
            end = "成功";
        }else if(jcqdz.equals("000E")&&lx.equals(getJCQDZLX(jcqdz))){
//            lx = "电流限值";
            end = "成功";
        }else if(jcqdz.equals("0010")&&lx.equals(getJCQDZLX(jcqdz))){
//            lx = "闭门时间";
            end = "成功";
        }else if(jcqdz.equals("0006")&&lx.equals(getJCQDZLX(jcqdz))){
//            lx = "清除故障";
            end = "成功";
        }else if(jcqdz.equals("0003")&&lx.equals(getJCQDZLX(jcqdz))){
//            lx = "输出控制";
            end = "成功";
        }else if(jcqdz.equals("00E0")&&lx.equals(getJCQDZLX(jcqdz))){
//            lx = "地址更改";
            end = "成功";
        }
        return end;
    }

    /**
     * 寄存器地址翻译-其他设置
     * @param jcqdz
     * @param ohter
     * @return
     */
    public static String getJCQDZLX_2(String jcqdz, String ohter, String other3){
        String lx = "未知";
        if(jcqdz.equals("0004")){
            if(ohter.equals("0000")){
                lx = "0点校准";
            }else if(ohter.equals("FFFF")){
                lx = "去皮";
            }else{
                lx = "砝码校准";
            }
        }else if(jcqdz.equals("0005")){
            if(ohter.equals("0001")){
                lx = "工作模式";
            }else if(ohter.equals("0002")){
                lx = "调试模式";
            }
        }else if(jcqdz.equals("0001")){
            if(other3.equals("01")){
                lx = "正转";
            }else if(other3.equals("03")){
                lx = "反转";
            }
        }else if(jcqdz.equals("0006")){
            lx = "清除故障";
        }
        return lx;
    }

    /**
     * 根据中文类型，判断是否对应寄存器--判断失误
     * @param jcqdz
     * @param ohter
     * @return
     */
    public static String getJCQDZLX_2FromLX(String lx, String jcqdz, String ohter, String ohter3){
        String end = "失败";
        if(jcqdz.equals("0004")&&lx.equals(getJCQDZLX_2(jcqdz, ohter, ohter3))){
//            if(ohter.equals("0000")){
////                lx = "0点校准";
//            }else if(ohter.equals("FFFF")){
////                lx = "去皮";
//            }else{
////                lx = "砝码校准";
//            }
            end = "成功";
        }else if(jcqdz.equals("0005")){
            if(ohter.equals("0001")&&lx.equals(getJCQDZLX_2(jcqdz, ohter, ohter3))){
//                lx = "工作模式";
                end = "成功";
            }else if(ohter.equals("0002")&&lx.equals(getJCQDZLX_2(jcqdz, ohter, ohter3))){
//                lx = "调试模式";
                end = "成功";
            }
        }else if(jcqdz.equals("0001")){
            if(ohter.equals("0001")&&lx.equals(getJCQDZLX_2(jcqdz, ohter, ohter3))){
//                lx = "正转";
                end = "成功";
            }else if(ohter.equals("0003")&&lx.equals(getJCQDZLX_2(jcqdz, ohter, ohter3))){
//                lx = "反转";
                end = "成功";
            }
        }
        return end;
    }

}
